package com.probiz.estore.eai.unit;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.probiz.estore.eai.unit.generated.Unit;

/**
 * 用于将EAI计量单位的信息保存到唯一实例中
 *
 */
public class UnitEaiHelper {
	protected final transient Log	logger	= LogFactory.getLog(getClass());
	private static UnitEaiHelper	unitEaiHelper			= new UnitEaiHelper();
	private Map<String ,Unit> unitMap = new HashMap<String ,Unit>();//key=Unit-code,value=Unit

	private UnitEaiHelper() {
	}
	/**
	 * 获得唯一实例
	 * 饿汉式初始化
	 * @return UnitEaiHelper
	 */
	public static UnitEaiHelper getInstance() {
		return unitEaiHelper;
	}
	/**
	 * 将EAI的数据导入到实例中
	 */
	public void importData(List<Unit> unitList){
		unitMap.clear();
		for(Unit unit: unitList){
			logger.debug("导入：unit name:"+unit.getName());
			unitMap.put(unit.getCode(), unit);
		}
		logger.debug("总共导入"+unitList.size()+"个计量单位");
		
	}
	/**
	 * 根据计量单位的code获得计量单位实体
	 * @param code
	 * @return Unit
	 */
	public Unit getUnitByCode(String code){
		if(unitMap.containsKey(code)){
			return unitMap.get(code);
		}
		return null;
	}
}
